

<title>用户管理</title>
  
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>用户管理</cite></a>
    </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-userfront-formlist">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <select name="status">
                        <option value="">不限</option>
                        <option value="Y">启用</option>
                        <option value="N">禁用</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">角色</label>
                <div class="layui-input-block">
                    <select name="role">
                        <option value="">不限</option>
                        <option value="ADMIN">系统管理员</option>
                        <option value="TENANT">租户</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">登录手机</label>
                <div class="layui-input-block">
                    <input type="text" name="userid" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input type="text" name="email" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
            </div>
        </div>
    </div>
    
    <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
            <!--<button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</button>-->
            <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
        </div>

        <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
        <script type="text/html" id="table-useradmin-webuser">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
            <!-- 这里同样支持 laytpl 语法，如： -->
            {{#  if(d.isEnabled == 'Y'){ }}
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="disable"><i class="layui-icon layui-icon-disabled"></i>禁用</a>
            {{#  }else{ }}
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="enable"><i class="layui-icon layui-icon-ok-circle"></i>启用</a>
            {{#  } }}
        </script>
        <script type="text/html" id="table-useradmin-templet-status">
            {{#  if(d.isEnabled == 'Y'){ }}
            <span style="color: green">启用</span>
            {{#  }else{ }}
            <span style="color: red">禁用</span>
            {{#  } }}
        </script>
        <script type="text/html" id="table-useradmin-templet-role">
            {{#  if(d.role == 'ADMIN'){ }}
            <span style="">系统管理员</span>
            {{#  }else if(d.role == 'TENANT'){ }}
            <span style="">租户</span>
            {{#  }else{ }}
            <span style="">未知用户</span>
            {{#  } }}
        </script>
    </div>
  </div>
</div>

<script>
layui.use('useradmin', layui.factory('useradmin')).use(['admin', 'useradmin', 'table'], function(){
  var $ = layui.$
      , admin = layui.admin
      , setter = layui.setter
  ,view = layui.view
  ,table = layui.table
  ,form = layui.form;
  
  form.render(null, 'layadmin-userfront-formlist');
  
  //开启搜索
  form.on('submit(LAY-user-front-search)', function(data){
    var field = data.field;
    
    //执行重载
    table.reload('LAY-user-manage', {
      where: field
    });
  });

  //事件
  var active = {
    batchdel: function(){
      var checkStatus = table.checkStatus('LAY-user-manage')
      ,checkData = checkStatus.data; //得到选中的数据

      if(checkData.length === 0){
        return layer.msg('请选择数据');
      }
      
      layer.prompt({
        formType: 1
        ,title: '敏感操作，请验证口令'
      }, function(value, index){
        layer.close(index);
        
        layer.confirm('确定删除吗？', function(index) {
          
          //执行 Ajax 后重载
          /*
          admin.req({
            url: 'xxx'
            //,……
          });
          */
          table.reload('LAY-user-manage');
          layer.msg('已删除');
        });
      });
    }
    ,add: function(){
      admin.popup({
        title: '添加用户'
        ,area: ['500px', '450px']
        ,id: 'LAY-popup-user-add'
        ,success: function(layero, index){
          view(this.id).render('user/user/userform').done(function(){
            form.render(null, 'layuiadmin-form-useradmin');
            
            //开启提交
              form.on('submit(LAY-user-front-submit)', function (data) {
              var field = data.field; //获取提交的字段

                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.ajax({
                    url: setter.baseConfig.apiHost + '/api/user/add' //实际使用请改成服务端真实接口
                    , type: 'post'
                    , contentType: "application/json"
                    , data: JSON.stringify(field)
                    , beforeSend: function (xhr) {
                        xhr.setRequestHeader("Authorization", 'Bearer ' + layui.data(setter.tableName)[setter.request.tokenName]);
                    }
                    , success: function (res) {
                        if (res.errorCode != '00000') {
                            layer.msg(res.errorMessage, {
                                offset: '15px'
                                , icon: 2
                                , time: 3000
                            });

                            return false;
                        }

                        //登入成功的提示与跳转
                        layer.msg('添加成功', {
                            offset: '15px'
                            , icon: 1
                            , time: 3000
                        });

                        layui.table.reload('LAY-user-manage'); //重载表格
                        layer.close(index); //执行关闭
                    }
                }); 
            });
          });
        }
      });
    }
  };
  
  $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>